const mta = require('../../../mta_sdk/mta_analysis.js');
var utils=require('../../../utils/util')
var ctx = null;
const app = getApp();
var pid = '';

Page({

  data: {
    shareImg: '',
    shared: 0,
    ajaxSuccess: false
  },

  onLoad: function (opt) {
    mta.Page.init();
    console.log(opt);
    if (opt.shared == 1) {
      this.setData({
        shared: 1,
        shareImg: opt.shareImg || '',
        ajaxSuccess: true
      });

    }
    if (opt.pid) {

      this.makeImg(opt.address, opt.userName, opt.lastRom, opt.lastPrice, opt.lastColor,opt.date,opt.status)

    }
    
  },

  makeImg(address, userName, lastRom, lastPrice, lastColor,date,status) {
    var me = this;
    if (me.data.shared == 1) {
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    });

    console.log(address,userName,lastRom,lastPrice,lastColor)

    var sysInfo = wx.getSystemInfoSync();
    var ft = sysInfo.windowWidth * .06;
    var query = wx.createSelectorQuery();
    ctx = wx.createCanvasContext("mycanvas");
    query.select('#canvas').boundingClientRect(); //获取canvas的元素信息
    query.exec(res => {

      var w = res[0].width;
      var h = res[0].height;


      
      ctx.drawImage('/images/iphone_canvas.jpg', 0, 0, w, h);
      ctx.drawImage(`/images/${lastColor}.jpg`, w /20,h/1.77, w/4.8, w/4.8);
      var titleLeft=w/25;
      var titleValLeft=w /4.6;
      var titleSize=12;
      var titleColor="#999999";
      var valColor="#222";
      var st=status || '待发货';
      var d = date || ''
      var t = utils.formatTime(new Date())
      //信息
      var mess=''
      if(st=='待发货'){
        //mess=`您的订单预计${date.substring(6,7)}月${parseInt(date.substring(8,10))+2}日送达你手中`
        mess = "您的包裹已出库"
      }
      else if(st=='已发货'){
        mess=`您的订单订单正在配送中，请耐心等待`
      }
      else if(st=='已签收'){
        mess=`您的包裹已被签收，签收人：${userName.substring(0,1)}`
      }
      else{
        mess="您的包裹已出库"
      }

      //状态栏时间
      ctx.setFontSize(titleSize);
      ctx.setFillStyle("#525252");
      ctx.fillText(t, w / 25, h /43);

      ctx.setFontSize(titleSize);
      ctx.setFillStyle(valColor);
      ctx.fillText(mess, w /8, h/6.9);

      //绘制灰色标题
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText('订单状态 :', titleLeft, h/4.7);
      ctx.setFontSize(titleSize);
      ctx.setFillStyle("#e93b3d");
      ctx.fillText(st, titleValLeft, h/4.7);

      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText('订单编号 :', titleLeft, h/4.1);
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(valColor);
      ctx.fillText(parseInt(Math.random()*10000000000), titleValLeft, h/4.1);

      //时间
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText('下单时间 :', titleLeft, h/3.65);
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(valColor);
      
      ctx.fillText(`${d} ${t}`, titleValLeft, h /3.65);


      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText('商品金额 :', titleLeft, h/3);
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(valColor);
      ctx.fillText(`¥${lastPrice}.00`, titleValLeft, h/3);

      let mob2=parseInt(Math.random()*(1 - 9 + 1) + 9)
      let mob3=parseInt(Math.random()*10000)
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText('收货人 :', titleLeft, h/2.74);
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(valColor);
      ctx.fillText(`${userName}  13${mob2}****${mob3}`, titleValLeft, h/2.74);
      
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText('收货地址 :', titleLeft, h/2.5);
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(valColor);
      ctx.fillText(address, titleValLeft, h/2.5);
      //地址模糊部分
      ctx.drawImage(`/images/blur.jpg`, titleValLeft-5,h/2.45, 299*0.5, 39*0.5);

      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText('配送方式 :', titleLeft, h/2.17);
      ctx.setFontSize(titleSize);
      ctx.setFillStyle(valColor);
      ctx.fillText(`顺丰快递`, titleValLeft, h/2.17);
      //价格
      ctx.setFontSize(titleSize);
      ctx.setFillStyle("#e93b3d");
      ctx.fillText(`¥${lastPrice}.00`, w-70, h/1.68);

      ctx.setFontSize(titleSize);
      ctx.setFillStyle(titleColor);
      ctx.fillText(`x1`, w-30, h/1.6);

     //规格

      ctx.setFontSize(14);
      ctx.setFillStyle("#000");
      var re=lastColor.replace(/_/g,' ');
      ctx.fillText(`iPhone ${re}色 ${lastRom} `, w /4,h/1.68);
      
      ctx.setFontSize(14);
      ctx.setFillStyle("#000");
      ctx.fillText(`移动联通电信4G手机`, w /4, h/1.6);


      ctx.setFontSize(13);
      ctx.setFillStyle("#777");
      let rex=lastColor.replace('XS_MAX_','').replace('XS_','').replace('XR_','')
      ctx.fillText(`${rex}色  ${lastRom}`, w /4, h/1.53);

      ctx.draw(false, function () {
        app.buldingImg("mycanvas", callBack => {
          me.setData({
            shareImg: callBack.data.url,
            ajaxSuccess: true
          });
          wx.hideLoading();
        }); 
      });
      
    })

  },
  //保存图片
  saveImg() {
    app.savePic("mycanvas");
  },
  onShareAppMessage: function () {
    console.log(this.data.shareImg)
    return {
      title: '新iphone订单生成',
      desc: '新iphone订单生成',
      path: `/pages/${pid}/${pid}_detail/${pid}_detail?shared=1&shareImg=${this.data.shareImg}`,
      imageUrl: this.data.shareImg,
      Success: function () {

      }
    }
  },
  onUnload() {
    wx.removeStorageSync("coverpic")
  },
  toPlay() {
    wx.navigateTo({
      url: `/pages/${pid}/${pid}_make/${pid}_make?pid=${pid}`,
    })
  },
  toHome() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  }
})